package com.alibaba.basic.util;


import com.alibaba.basic.jwt.JwtUtils;
import com.alibaba.basic.jwt.LoginData;
import com.alibaba.basic.jwt.Payload;
import com.alibaba.basic.jwt.RsaUtils;
import com.alibaba.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    //获取登录人信息
    public static Logininfo getLoginUser(HttpServletRequest request){
        try {
            //获取请求头
            String token = request.getHeader("token");
            //获取公钥
            PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
            if (payload != null){
                Logininfo logininfo = payload.getLoginData().getLogininfo();
                return logininfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
